/**
 * Created by DevilsEyes on 2016/3/14.
 */
'use strict';

define(['text!path/addAddress/tmp.html', 'address', 'layer'], function (page, address, layer) {

    var render = template.compile(page);
    var provinceList = address.getSelection();
    var render_CitySelection = template.compile(
        '<option value="0" selected disabled>未选择</option>' +
        '{{each cityList}}' +
        '<option value="{{$value.code}}">{{$value.name}}</option>' +
        '{{/each}}'
    );
    var symbol = {};

    var apiList = {
        addAddress: api + 'user/address',
        editAddress: api + 'user/address'
    };

    var vm = new Page.creat({
        $id: 'addAddress',
        $model: {
            data: {},
            $: {},
            _id: null,
            add: function () {

                vm._id = null;

                vm.data = {
                    name: '',
                    phone: '',
                    place: '',
                    province: '',
                    city: '',
                    provinceList: provinceList
                };

                $('#page').html(render(vm.data));
                ex.async(function () {

                    vm.bindEvent(vm.event);
                    vm.bind$(vm.$);

                });

            },
            edit: function (id) {

                var ADDRESS = db.get('Address' + id);
                if (ADDRESS == null) {
                    //todo 跳到错误页
                    throw 'ADDRESS is NULL';
                }

                var obj = ADDRESS;
                vm._id = id;

                vm.data = {
                    name: obj.receiver_name,
                    phone: obj.phone,
                    place: obj.place,
                    province: obj.province,
                    city: obj.city,
                    provinceList: provinceList,
                    cityList: obj.province && address.getSelection(obj.province, 1)
                };
                $('#page').html(render(vm.data));

                ex.async(function () {

                    vm.bindEvent(vm.event);
                    vm.bind$(vm.$);

                });

            },
            event: {
                clickfb: function () {
                    if (vm.$.fbutton.hasClass('un') || symbol.pending)return;
                    symbol.pending = true;
                    var POSTDATA = {
                        receiver_name: vm.$.name.val(),
                        phone: vm.$.phone.val(),
                        province: vm.$.provinceList.val(),
                        city: vm.$.cityList.val(),
                        place: vm.$.place.val()
                    };
                    if (vm._id)POSTDATA._id = vm._id;
                    ex.jsonp({
                        url: vm._id ? apiList.editAddress : apiList.addAddress,
                        data: POSTDATA,
                        method: vm._id ? 'PUT' : 'POST',
                        success: function (obj) {
                            if (!vm.isThisPage())return;
                            //console.log(obj);
                            if (obj.code == 0) {
                                var orderInfo = db.get('confirmOrder');
                                if (orderInfo) {
                                    orderInfo.address = obj.data || POSTDATA;
                                    db.set('confirmOrder', orderInfo);
                                }
                                layer.msg('添加收货地址成功');
                                history.back();
                            }
                            else {
                                layer.msg(obj.msg);
                            }
                        }
                    })
                },
                checkAvaliable: function () {
                    if (
                        vm.$.name.val().length > 0
                        && vm.$.phone.val().length == 11
                        && vm.$.provinceList.val() > 0
                        && vm.$.cityList.val() > 0
                        && vm.$.place.val().length > 5
                    ) {
                        vm.$.fbutton.removeClass('un');
                    } else {
                        vm.$.fbutton.addClass('un');
                    }

                },
                selectProvince: function () {
                    var provinceCode = $(this).val();
                    vm.event.checkAvaliable();
                    vm.$.cityList.html(
                        render_CitySelection({
                            cityList: address.getSelection(provinceCode, 1)
                        })
                    );
                }
            }
        }
    });

    return {
        add: function () {
            vm.$init({
                setTitle: '新增收货地址'
            });
            vm.add();
        },
        edit: function (params) {

            vm.$init({
                setTitle: '编辑收货地址'
            });

            vm.edit(params.id);
        }
    }
});